package com.gj.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gj.reggie.common.BaseContext;
import com.gj.reggie.dto.OrdersDto;
import com.gj.reggie.entity.*;
import com.gj.reggie.service.AddressBookService;
import com.gj.reggie.service.OrderDetailService;
import com.gj.reggie.service.OrdersService;
import com.gj.reggie.service.UserService;
import com.gj.reggie.utils.DataTimeUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 郭嘉
 * @date 2023/1/7 - 14:02
 */
@RestController
@Slf4j
@RequestMapping("/order")
public class OrdersController {

    @Autowired
    private OrdersService ordersService;
    @Autowired
    private UserService userService;
    @Autowired
    private AddressBookService addressBookService;
    @Autowired
    private OrderDetailService orderDetailService;


    /**
     * 用户下单
     * @param orders
     * @return
     */
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders){
        ordersService.submit(orders);
        return R.success("下单成功");
    }


    @GetMapping("/userPage")
    public R<Page> page(Integer page,Integer pageSize){
        Long userId= BaseContext.getCurrentId();
        //查询用户
        LambdaQueryWrapper<User> userLambdaQueryWrapper=new LambdaQueryWrapper<>();
        userLambdaQueryWrapper.eq(User::getId,userId);
        User user=userService.getOne(userLambdaQueryWrapper);

        Page<Orders> pageInfo=new Page<>(page,pageSize);
        Page<OrdersDto> dtoPage=new Page<>();

        LambdaQueryWrapper<Orders> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Orders::getUserId,user.getId());
        queryWrapper.orderByDesc(Orders::getOrderTime);
        ordersService.page(pageInfo,queryWrapper);

        BeanUtils.copyProperties(pageInfo,dtoPage,"records");

        List<Orders> records = pageInfo.getRecords();
        List<OrdersDto> ordersDtos=records.stream().map((item)->{
            OrdersDto ordersDto=new OrdersDto();
            BeanUtils.copyProperties(item,ordersDto);
            //查询地址
            LambdaQueryWrapper<AddressBook> addressBookLambdaQueryWrapper=new LambdaQueryWrapper<>();
            addressBookLambdaQueryWrapper.eq(AddressBook::getId,item.getAddressBookId());
            AddressBook addressBook=addressBookService.getOne(addressBookLambdaQueryWrapper);
            //查询订单明细
            LambdaQueryWrapper<OrderDetail> orderDetailLambdaQueryWrapper=new LambdaQueryWrapper<>();
            orderDetailLambdaQueryWrapper.eq(OrderDetail::getOrderId,item.getId());
            List<OrderDetail> orderDetailList=orderDetailService.list(orderDetailLambdaQueryWrapper);

            ordersDto.setUserName(user.getName());
            ordersDto.setPhone(user.getPhone());
            ordersDto.setAddress((addressBook.getProvinceName() == null ? "" : addressBook.getProvinceName())
                    + (addressBook.getCityName() == null ? "" : addressBook.getCityName())
                    + (addressBook.getDistrictName() == null ? "" : addressBook.getDistrictName())
                    + (addressBook.getDetail() == null ? "" : addressBook.getDetail()));
            ordersDto.setOrderDetails(orderDetailList);
            return ordersDto;

        }).collect(Collectors.toList());

        dtoPage.setRecords(ordersDtos);

        return R.success(dtoPage);
    }

    @GetMapping("/page")
    public R<Page> empPage(Integer page, Integer pageSize, Long number, String beginTime,String endTime){

        Page<Orders> pageInfo=new Page<>(page,pageSize);
        Page<OrdersDto> dtoPage=new Page<>();


        LambdaQueryWrapper<Orders> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(number!=null,Orders::getId,number);
        if(StringUtils.isNotEmpty(beginTime)){
            LocalDateTime startDataTime=DataTimeUtils.stringToLocalDateTime(beginTime);
            queryWrapper.ge(Orders::getOrderTime,startDataTime);
        }
        if(StringUtils.isNotEmpty(endTime)){
            LocalDateTime endDataTime=DataTimeUtils.stringToLocalDateTime(endTime);
            queryWrapper.ge(Orders::getOrderTime,endDataTime);
        }
        queryWrapper.orderByDesc(Orders::getOrderTime);
        ordersService.page(pageInfo,queryWrapper);

        BeanUtils.copyProperties(pageInfo,dtoPage,"records");

        List<Orders> records = pageInfo.getRecords();
        List<OrdersDto> ordersDtos=records.stream().map((item)->{
            OrdersDto ordersDto=new OrdersDto();
            BeanUtils.copyProperties(item,ordersDto);
            //查询用户
            LambdaQueryWrapper<User> userLambdaQueryWrapper=new LambdaQueryWrapper<>();
            userLambdaQueryWrapper.eq(User::getId,item.getUserId());
            User user=userService.getOne(userLambdaQueryWrapper);
            //查询地址
            LambdaQueryWrapper<AddressBook> addressBookLambdaQueryWrapper=new LambdaQueryWrapper<>();
            addressBookLambdaQueryWrapper.eq(AddressBook::getId,item.getAddressBookId());
            AddressBook addressBook=addressBookService.getOne(addressBookLambdaQueryWrapper);
            //查询订单明细
            LambdaQueryWrapper<OrderDetail> orderDetailLambdaQueryWrapper=new LambdaQueryWrapper<>();
            orderDetailLambdaQueryWrapper.eq(OrderDetail::getOrderId,item.getId());
            List<OrderDetail> orderDetailList=orderDetailService.list(orderDetailLambdaQueryWrapper);

            ordersDto.setUserName(user.getName());
            ordersDto.setPhone(user.getPhone());
            ordersDto.setAddress((addressBook.getProvinceName() == null ? "" : addressBook.getProvinceName())
                    + (addressBook.getCityName() == null ? "" : addressBook.getCityName())
                    + (addressBook.getDistrictName() == null ? "" : addressBook.getDistrictName())
                    + (addressBook.getDetail() == null ? "" : addressBook.getDetail()));
            ordersDto.setOrderDetails(orderDetailList);
            return ordersDto;

        }).collect(Collectors.toList());

        dtoPage.setRecords(ordersDtos);

        return R.success(dtoPage);
    }
}
